home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / PROGRAMMING / PERL.SPK / Perl5001 / Manual / perlbot_ht < prev    next >
Text File  |  1995-04-18  |  11KB  |  405 lines

  1. <!-- $RCSfile$$Revision$$Date$ -->
  2. <!-- $Log$ -->
  3. <HTML>
  4. <TITLE> PERLBOT </TITLE>
  5. <h2>NAME</h2>
  6. perlbot - Bag'o Object Tricks For Perl5 (the BOT)
  7. <p><h2>INTRODUCTION</h2>
  8. The following collection of tricks and hints is intended to whet curious
  9. appetites about such things as the use of instance variables and the
  10. mechanics of object and class relationships.  The reader is encouraged to
  11. consult relevant textbooks for discussion of Object Oriented definitions and
  12. methodology.  This is not intended as a comprehensive guide to Perl5's
  13. object oriented features, nor should it be construed as a style guide.
  14. <p>The Perl motto still holds:  There's more than one way to do it.
  15. <p><h2>INSTANCE VARIABLES</h2>
  16. An anonymous array or anonymous hash can be used to hold instance
  17. variables.  Named parameters are also demonstrated.
  18. <p><pre>
  19.         package Foo;
  20. </pre>
  21. <pre>
  22.         sub new {
  23.                 my $type = shift;
  24.                 my %params = @_;
  25.                 my $self = {};
  26.                 $self->{'High'} = $params{'High'};
  27.                 $self->{'Low'}  = $params{'Low'};
  28.                 bless $self;
  29.         }
  30. </pre>
  31. <pre>
  32.         package Bar;
  33. </pre>
  34. <pre>
  35.         sub new {
  36.                 my $type = shift;
  37.                 my %params = @_;
  38.                 my $self = [];
  39.                 $self->[0] = $params{'Left'};
  40.                 $self->[1] = $params{'Right'};
  41.                 bless $self;
  42.         }
  43. </pre>
  44. <pre>
  45.         package main;
  46. </pre>
  47. <pre>
  48.         $a = new Foo ( 'High' => 42, 'Low' => 11 );
  49.         print "High=$a->{'High'}\n";
  50.         print "Low=$a->{'Low'}\n";
  51. </pre>
  52. <pre>
  53.         $b = new Bar ( 'Left' => 78, 'Right' => 40 );
  54.         print "Left=$b->[0]\n";
  55.         print "Right=$b->[1]\n";
  56. </pre>
  57. <h2>SCALAR INSTANCE VARIABLES</h2>
  58. An anonymous scalar can be used when only one instance variable is needed.
  59. <p><pre>
  60.         package Foo;
  61. </pre>
  62. <pre>
  63.         sub new {
  64.                 my $type = shift;
  65.                 my $self;
  66.                 $self = shift;
  67.                 bless \$self;
  68.         }
  69. </pre>
  70. <pre>
  71.         package main;
  72. </pre>
  73. <pre>
  74.         $a = new Foo 42;
  75.         print "a=$$a\n";
  76. </pre>
  77. <h2>INSTANCE VARIABLE INHERITANCE</h2>
  78. This example demonstrates how one might inherit instance variables from a
  79. superclass for inclusion in the new class.  This requires calling the
  80. superclass's constructor and adding one's own instance variables to the new
  81. object.
  82. <p><pre>
  83.         package Bar;
  84. </pre>
  85. <pre>
  86.         sub new {
  87.                 my $self = {};
  88.                 $self->{'buz'} = 42;
  89.                 bless $self;
  90.         }
  91. </pre>
  92. <pre>
  93.         package Foo;
  94.         @ISA = qw( Bar );
  95. </pre>
  96. <pre>
  97.         sub new {
  98.                 my $self = new Bar;
  99.                 $self->{'biz'} = 11;
  100.                 bless $self;
  101.         }
  102. </pre>
  103. <pre>
  104.         package main;
  105. </pre>
  106. <pre>
  107.         $a = new Foo;
  108.         print "buz = ", $a->{'buz'}, "\n";
  109.         print "biz = ", $a->{'biz'}, "\n";
  110. </pre>
  111. <h2>OBJECT RELATIONSHIPS</h2>
  112. The following demonstrates how one might implement "containing" and "using"
  113. relationships between objects.
  114. <p><pre>
  115.         package Bar;
  116. </pre>
  117. <pre>
  118.         sub new {
  119.                 my $self = {};
  120.                 $self->{'buz'} = 42;
  121.                 bless $self;
  122.         }
  123. </pre>
  124. <pre>
  125.         package Foo;
  126. </pre>
  127. <pre>
  128.         sub new {
  129.                 my $self = {};
  130.                 $self->{'Bar'} = new Bar ();
  131.                 $self->{'biz'} = 11;
  132.                 bless $self;
  133.         }
  134. </pre>
  135. <pre>
  136.         package main;
  137. </pre>
  138. <pre>
  139.         $a = new Foo;
  140.         print "buz = ", $a->{'Bar'}->{'buz'}, "\n";
  141.         print "biz = ", $a->{'biz'}, "\n";
  142. </pre>
  143. <h2>OVERRIDING SUPERCLASS METHODS</h2>
  144. The following example demonstrates how one might override a superclass
  145. method and then call the method after it has been overridden.  The
  146. Foo::Inherit class allows the programmer to call an overridden superclass
  147. method without actually knowing where that method is defined.
  148. <p><pre>
  149.         package Buz;
  150.         sub goo { print "here's the goo\n" }
  151. </pre>
  152. <pre>
  153.         package Bar; @ISA = qw( Buz );
  154.         sub google { print "google here\n" }
  155. </pre>
  156. <pre>
  157.         package Baz;
  158.         sub mumble { print "mumbling\n" }
  159. </pre>
  160. <pre>
  161.         package Foo;
  162.         @ISA = qw( Bar Baz );
  163.         @Foo::Inherit::ISA = @ISA;  # Access to overridden methods.
  164. </pre>
  165. <pre>
  166.         sub new { bless [] }
  167.         sub grr { print "grumble\n" }
  168.         sub goo {
  169.                 my $self = shift;
  170.                 $self->Foo::Inherit::goo();
  171.         }
  172.         sub mumble {
  173.                 my $self = shift;
  174.                 $self->Foo::Inherit::mumble();
  175.         }
  176.         sub google {
  177.                 my $self = shift;
  178.                 $self->Foo::Inherit::google();
  179.         }
  180. </pre>
  181. <pre>
  182.         package main;
  183. </pre>
  184. <pre>
  185.         $foo = new Foo;
  186.         $foo->mumble;
  187.         $foo->grr;
  188.         $foo->goo;
  189.         $foo->google;
  190. </pre>
  191. <h2>USING RELATIONSHIP WITH SDBM </h2>
  192. This example demonstrates an interface for the SDBM class.  This creates a
  193. "using" relationship between the SDBM class and the new class Mydbm.
  194. <p><pre>
  195.         use SDBM_File;
  196.         use POSIX;
  197. </pre>
  198. <pre>
  199.         package Mydbm;
  200. </pre>
  201. <pre>
  202.         sub TIEHASH {
  203.             my $self = shift;
  204.             my $ref  = SDBM_File->new(@_);
  205.             bless {'dbm' => $ref};
  206.         }
  207.         sub FETCH {
  208.             my $self = shift;
  209.             my $ref  = $self->{'dbm'};
  210.             $ref->FETCH(@_);
  211.         }
  212.         sub STORE {
  213.             my $self = shift; 
  214.             if (defined $_[0]){
  215.                 my $ref = $self->{'dbm'};
  216.                 $ref->STORE(@_);
  217.             } else {
  218.                 die "Cannot STORE an undefined key in Mydbm\n";
  219.             }
  220.         }
  221. </pre>
  222. <pre>
  223.         package main;
  224. </pre>
  225. <pre>
  226.         tie %foo, Mydbm, "Sdbm", O_RDWR|O_CREAT, 0640;
  227.         $foo{'bar'} = 123;
  228.         print "foo-bar = $foo{'bar'}\n";
  229. </pre>
  230. <pre>
  231.         tie %bar, Mydbm, "Sdbm2", O_RDWR|O_CREAT, 0640;
  232.         $bar{'Cathy'} = 456;
  233.         print "bar-Cathy = $bar{'Cathy'}\n";
  234. </pre>
  235. <h2>THINKING OF CODE REUSE</h2>
  236. One strength of Object-Oriented languages is the ease with which old code
  237. can use new code.  The following examples will demonstrate first how one can
  238. hinder code reuse and then how one can promote code reuse.
  239. <p>This first example illustrates a class which uses a fully-qualified method
  240. call to access the "private" method BAZ().  The second example will show
  241. that it is impossible to override the BAZ() method.
  242. <p><pre>
  243.         package FOO;
  244. </pre>
  245. <pre>
  246.         sub new { bless {} }
  247.         sub bar {
  248.                 my $self = shift;
  249.                 $self->FOO::private::BAZ;
  250.         }
  251. </pre>
  252. <pre>
  253.         package FOO::private;
  254. </pre>
  255. <pre>
  256.         sub BAZ {
  257.                 print "in BAZ\n";
  258.         }
  259. </pre>
  260. <pre>
  261.         package main;
  262. </pre>
  263. <pre>
  264.         $a = FOO->new;
  265.         $a->bar;
  266. </pre>
  267. Now we try to override the BAZ() method.  We would like FOO::bar() to call
  268. GOOP::BAZ(), but this cannot happen since FOO::bar() explicitly calls
  269. FOO::private::BAZ().
  270. <p><pre>
  271.         package FOO;
  272. </pre>
  273. <pre>
  274.         sub new { bless {} }
  275.         sub bar {
  276.                 my $self = shift;
  277.                 $self->FOO::private::BAZ;
  278.         }
  279. </pre>
  280. <pre>
  281.         package FOO::private;
  282. </pre>
  283. <pre>
  284.         sub BAZ {
  285.                 print "in BAZ\n";
  286.         }
  287. </pre>
  288. <pre>
  289.         package GOOP;
  290.         @ISA = qw( FOO );
  291.         sub new { bless {} }
  292. </pre>
  293. <pre>
  294.         sub BAZ {
  295.                 print "in GOOP::BAZ\n";
  296.         }
  297. </pre>
  298. <pre>
  299.         package main;
  300. </pre>
  301. <pre>
  302.         $a = GOOP->new;
  303.         $a->bar;
  304. </pre>
  305. To create reusable code we must modify class FOO, flattening class
  306. FOO::private.  The next example shows a reusable class FOO which allows the
  307. method GOOP::BAZ() to be used in place of FOO::BAZ().
  308. <p><pre>
  309.         package FOO;
  310. </pre>
  311. <pre>
  312.         sub new { bless {} }
  313.         sub bar {
  314.                 my $self = shift;
  315.                 $self->BAZ;
  316.         }
  317. </pre>
  318. <pre>
  319.         sub BAZ {
  320.                 print "in BAZ\n";
  321.         }
  322. </pre>
  323. <pre>
  324.         package GOOP;
  325.         @ISA = qw( FOO );
  326. </pre>
  327. <pre>
  328.         sub new { bless {} }
  329.         sub BAZ {
  330.                 print "in GOOP::BAZ\n";
  331.         }
  332. </pre>
  333. <pre>
  334.         package main;
  335. </pre>
  336. <pre>
  337.         $a = GOOP->new;
  338.         $a->bar;
  339. </pre>
  340. <h2>CLASS CONTEXT AND THE OBJECT</h2>
  341. Use the object to solve package and class context problems.  Everything a
  342. method needs should be available via the object or should be passed as a
  343. parameter to the method.
  344. <p>A class will sometimes have static or global data to be used by the
  345. methods.  A subclass may want to override that data and replace it with new
  346. data.  When this happens the superclass may not know how to find the new
  347. copy of the data.
  348. <p>This problem can be solved by using the object to define the context of the
  349. method.  Let the method look in the object for a reference to the data.  The
  350. alternative is to force the method to go hunting for the data ("Is it in my
  351. class, or in a subclass?  Which subclass?"), and this can be inconvenient
  352. and will lead to hackery.  It is better to just let the object tell the
  353. method where that data is located.
  354. <p><pre>
  355.         package Bar;
  356. </pre>
  357. <pre>
  358.         %fizzle = ( 'Password' => 'XYZZY' );
  359. </pre>
  360. <pre>
  361.         sub new {
  362.                 my $self = {};
  363.                 $self->{'fizzle'} = \%fizzle;
  364.                 bless $self;
  365.         }
  366. </pre>
  367. <pre>
  368.         sub enter {
  369.                 my $self = shift;
  370.         
  371.                 # Don't try to guess if we should use %Bar::fizzle
  372.                 # or %Foo::fizzle.  The object already knows which
  373.                 # we should use, so just ask it.
  374.                 #
  375.                 my $fizzle = $self->{'fizzle'};
  376. </pre>
  377.  
  378. <listing>
  379.         print "The word is ", $fizzle->{'Password'}, "\n"; 
  380.     } 
  381. </listing>
  382. <pre>
  383.         package Foo;
  384.         @ISA = qw( Bar );
  385. </pre>
  386. <pre>
  387.         %fizzle = ( 'Password' => 'Rumple' );
  388. </pre>
  389. <pre>
  390.         sub new {
  391.                 my $self = Bar->new;
  392.                 $self->{'fizzle'} = \%fizzle;
  393.                 bless $self;
  394.         }
  395. </pre>
  396. <pre>
  397.         package main;
  398. </pre>
  399. <pre>
  400.         $a = Bar->new;
  401.         $b = Foo->new;
  402.         $a->enter;
  403.         $b->enter;
  404. </pre>
  405.